package com.demo.proxy;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import com.demo.dao.TestDao;
import com.demo.dao.impl.TestDaoImpl;

/**
 * CGLIB动态代理,针对类做代理
 * @author HUZHAN150
 *
 */
public class TestCglib {
	
	public static void main(String[] args) {
		CglibProxy proxy = new CglibProxy();
		
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(TestDaoImpl.class);
		enhancer.setCallback(proxy);
		
		TestDao dao = (TestDao) enhancer.create();
		dao.delete();
	}

}


class CglibProxy implements MethodInterceptor{

	public Object intercept(Object object, Method method, Object[] objs,
			MethodProxy proxy) throws Throwable {
		String methodName = method.getName();
		System.out.println(methodName + "()开始时间:" + System.currentTimeMillis());
		proxy.invokeSuper(object, objs);
		System.out.println(methodName + "()结束时间:" + System.currentTimeMillis());
		return object;
	}
	
}